{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 步进驱动模块API文档"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 目录\n",
    "- [初始化步进驱动模块对象](#初始化步进驱动模块对象)\n",
    "- [获取API版本](#获取API版本)\n",
    "- [设置步进模块CAN_ID](#设置步进模块CAN_ID)\n",
    "- [设置细分](#设置细分)\n",
    "- [设置最大速度，加速度](#设置最大速度，加速度)\n",
    "- [步进电机使能](#步进电机使能)\n",
    "- [步进电机失能](#步进电机失能)\n",
    "- [停车](#停车)\n",
    "- [设置步进电机位置](#设置步进电机位置)\n",
    "- [设置步进电机速度](#设置步进电机速度)\n",
    "- [读取步进电机位置](#读取步进电机位置)\n",
    "- [读取步进电机速度](#读取步进电机速度)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化步进驱动模块对象\n",
    "\n",
    "出厂ID：0x22\n",
    "\n",
    "默认步距角1.8°\n",
    "\n",
    "参数：\n",
    "- reduction: 减速比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from protobot.can_bus import Robot\n",
    "from protobot.can_bus.nodes import StepperBoardFactory\n",
    "robot = Robot()\n",
    "stepper_board = robot.add_device('stepper', StepperBoardFactory(), 0x22, reduction = 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取API版本\n",
    "`get_api_ver(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "- timeout: 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值：\n",
    "- API版本dict\n",
    "    - device_uuid: 设备uuid\n",
    "    - main_version: 主版本号\n",
    "    - sub_version: 副版本号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'device_uuid': 98, 'main_version': 0, 'sub_version': 3}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stepper_board.get_api_ver(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设置步进模块CAN_ID\n",
    "`set_node_id(node_id)`\n",
    "\n",
    "参数:\n",
    "- node_id: 新CAN_ID (0x01~0x3F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.set_node_id(0x22)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设置细分\n",
    "`set_subdivision(subdivision)`\n",
    "\n",
    "参数:\n",
    "- subdivision: 细分(8,16,32,64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.set_subdivision(8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设置最大速度，加速度\n",
    "`set_vel_acc_limit`\n",
    "\n",
    "参数：\n",
    "- max_vel: 最大速度(rad/s), 默认值2.5*pi/s(减速比=1)\n",
    "- acc: 加速度(rad/s^2), 默认值 10*pi/s^2(减速比=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.set_vel_acc_limit(3.14/2, 3.14*2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 步进电机使能\n",
    "`enable()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.enable()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 步进电机失能\n",
    "`disable()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.disable()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 停车\n",
    "`stop()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设置步进电机位置\n",
    "`set_pos(position)`\n",
    "\n",
    "参数：\n",
    "- position: 目标位置(rad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.set_pos(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设置步进电机速度\n",
    "`set_vel(velocity)`\n",
    "\n",
    "参数:\n",
    "- velocity: 目标速度(rad/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stepper_board.set_vel(-3.14)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取步进电机位置\n",
    "`get_pos(timeout)`\n",
    "\n",
    "参数：\n",
    "- timeout\n",
    "\n",
    "返回值：\n",
    "- 实时电机位置(rad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "47.652070068731682"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stepper_board.get_pos()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读取步进电机速度\n",
    "`get_vel(timeout)`\n",
    "\n",
    "参数:\n",
    "- timeout\n",
    "\n",
    "返回值：\n",
    "- 实时电机速度(rad/s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.5699999990183853"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stepper_board.get_vel()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
